package com.leoren.run.activity.main;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.leoren.run.R;
import com.leoren.run.activity.announcement.AnnounceAdapter;
import com.leoren.run.common.ServerResponse;
import com.leoren.run.common.UrlConstants;
import com.leoren.run.dbbean.UserDB;
import com.leoren.run.log.L;
import com.leoren.run.utils.GsonUtil;
import com.leoren.run.utils.Http.OkHttp.CommonOkHttpClient;
import com.leoren.run.utils.Http.OkHttp.exception.OkHttpException;
import com.leoren.run.utils.Http.OkHttp.listener.DisposeDataHandle;
import com.leoren.run.utils.Http.OkHttp.listener.DisposeDataListener;
import com.leoren.run.utils.Http.OkHttp.request.CommonRequest;
import com.leoren.run.utils.ToastUtil;
import com.leoren.run.vo.AnnounceVo;
import com.leoren.run.vo.User;

import org.litepal.LitePal;

import java.util.List;

import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * 系统公布Fragment
 */
public class AnnouncementFragment extends Fragment {

    private View rootView;

    private ProgressDialog progressDialog;


    private LinearLayoutManager manager;
    private AnnounceAdapter adapter;
    private Toolbar toolbar;
    private RecyclerView announceListView;
    private TextView noResultView;
    private List<AnnounceVo> announceVos;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_announce, container, false);

        initViews();
        initData();

        return rootView;
    }

    private void initViews(){
        toolbar = rootView.findViewById(R.id.announce_toolbar_fragment);
        announceListView = rootView.findViewById(R.id.announce_list);
        noResultView = rootView.findViewById(R.id.announce_result);
    }

    /**
     * 网络请求系统公布数据
     */
    private void initData(){
        showProgressDialog();
        UserDB userDB = LitePal.findFirst(UserDB.class);
        RequestBody body = new FormBody.Builder()
                .add("userId", String.valueOf(userDB.getTagId()))
                .build();
        Request request = CommonRequest.createPostRequest(UrlConstants.GET_ALL_ANNOUNCE, body);
        CommonOkHttpClient.post(request, new DisposeDataHandle(new DisposeDataListener() {
            @Override
            public void onSuccess(String responseStr) {
                L.e(responseStr);
                closeProgressDialog();
                ServerResponse<List<AnnounceVo>> response = GsonUtil.formatJsonToAnnounceVoList(responseStr);
                if(response != null && response.isSuccess() && response.getData().size() > 0){
                    announceVos = response.getData();
                    setDate();
                    ToastUtil.MakeToast(response.getMsg());
                    return;
                }else {
                    noResult();
                    ToastUtil.MakeToast(response.getMsg());
                }
            }

            @Override
            public void onFailure(OkHttpException e) {
                closeProgressDialog();
            }
        }));
    }

    /**
     * 设置系统公布列表  展示信息
     */
    private void setDate() {
        adapter = new AnnounceAdapter(this.getContext(), announceVos);
        manager = new LinearLayoutManager(this.getContext());
        announceListView.setVisibility(View.VISIBLE);
        announceListView.setAdapter(adapter);
        announceListView.setLayoutManager(manager);
        adapter.notifyDataSetChanged();
    }

    private void noResult(){
        announceListView.setVisibility(View.GONE);
        noResultView.setVisibility(View.VISIBLE);
    }

    /**
     * 显示进度对话框
     */
    private void showProgressDialog(){
        if(progressDialog == null){
            progressDialog = new ProgressDialog(this.getContext());
            progressDialog.setMessage("正在加载...");
            progressDialog.setCanceledOnTouchOutside(false);
        }
        progressDialog.show();
    }

    /**
     * 关闭进度对话框
     */
    private void closeProgressDialog(){
        if(progressDialog != null){
            progressDialog.dismiss();
        }
    }

}
